package com.tencent.qcloud.tim.uikit.utils;

import android.app.Activity;
import android.app.Dialog;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.listener.OnResultCallbackListener;
import com.luck.picture.lib.tools.SdkVersionUtils;
import com.manor.common.interfaces.CommonCallback;
import com.manor.common.upload.UploadBean;
import com.manor.common.upload.UploadCallback;
import com.manor.common.upload.UploadQnImpl;
import com.manor.common.upload.UploadStrategy;
import com.manor.common.utils.DialogUitl;
import com.manor.common.utils.GlideEngine;
import com.manor.common.utils.StringUtil;
import com.tencent.qcloud.tim.uikit.R;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;

/**
 * @version 1.0
 * @date 2020/5/20  16:32
 */
public class UploadAvatarUtil {

    private UploadStrategy mUploadStrategy;
    private String mAvatarRemoteFileName;
    public static UploadAvatarUtil instance = null;

    static {
        if (instance == null) {
            instance = new UploadAvatarUtil();
        }
    }



    public void editAvatar(@NonNull final Activity mContext, final boolean isAvatar, final CommonCallback<String> callback) {
        DialogUitl.showStringArrayDialog(mContext, new Integer[]{
                R.string.camera, R.string.alumb}, new DialogUitl.StringArrayDialogCallback() {
            @Override
            public void onItemClick(String text, int tag) {
                if (tag == R.string.camera) {
                        UploadAvatarUtil.this.selectCamera(mContext, callback, isAvatar);
                } else {
                        UploadAvatarUtil.this.selectLocalImage(mContext, callback, isAvatar);
                }
            }
        });
    }


    /**
     * 选择拍照
     */
    public void selectCamera(final Activity mContext, final CommonCallback<String> callback, boolean isAvatar) {
        PictureSelector.create(mContext)
                .openCamera(PictureMimeType.ofImage())// 单独拍照，也可录像或也可音频 看你传入的类型是图片or视频
                .selectionData(null)// 是否传入已选图片
                .isCamera(true)// 是否显示拍照按钮
                .isGif(false)
                .isOpenClickSound(false)// 是否开启点击声音
                .isCompress(true)// 是否压缩
                .isEnableCrop(isAvatar)
                .withAspectRatio(600,600)
                .cutOutQuality(100)
                .minimumCompressSize(2048)
                .cropImageWideHigh(600,600)
                .circleDimmedLayer(true)
                .showCropFrame(false)
                .showCropGrid(false)
                .forResult(new OnResultCallbackListener<LocalMedia>() {
                    @Override
                    public void onResult(List<LocalMedia> result) {
                        if(StringUtil.isListNotEmpty(result)){
                            String path = SdkVersionUtils.checkedAndroid_Q()? result.get(0).getAndroidQToPath():
                                    result.get(0).getPath();
                            uploadAvatarImage(mContext, new File(path),callback);

                        }
                    }

                    @Override
                    public void onCancel() {

                    }
                });

    }

    public void selectLocalImage(final Activity mContext, final CommonCallback<String> callback, boolean isAvatar){
        boolean isAva = false;
        PictureSelector.create(mContext)
                .openGallery(PictureMimeType.ofImage())
                .imageEngine(GlideEngine.createGlideEngine())
                .minSelectNum(1)
                .maxSelectNum(1)
                .selectionData(null)
                .isCamera(false)
                .isGif(false)
                .isOpenClickSound(false)
                .isCompress(true)// 是否压缩
                .isEnableCrop(isAvatar)
                .withAspectRatio(600,600)
                .cutOutQuality(100)
                .minimumCompressSize(2048)
                .cropImageWideHigh(600,600)
                .circleDimmedLayer(true)
                .showCropFrame(false)
                .showCropGrid(false)
                .forResult(new OnResultCallbackListener<LocalMedia>() {
                    @Override
                    public void onResult(List<LocalMedia> result) {
                        if(StringUtil.isListNotEmpty(result)){
                            String path = SdkVersionUtils.checkedAndroid_Q()? result.get(0).getAndroidQToPath():
                                    result.get(0).getPath();

                            uploadAvatarImage(mContext, new File(path),callback);
                        }
                    }

                    @Override
                    public void onCancel() {

                    }
                });
    }







    /**
     * 上传头像
     */
    public void uploadAvatarImage(Activity mContext, File upFile, final CommonCallback<String> commonCallback) {
        final Dialog mLoading = DialogUitl.loadingDialog(mContext);
        mLoading.show();
        if (mUploadStrategy == null) {
            mUploadStrategy = new UploadQnImpl(mContext);
        }
        List<UploadBean> list = new ArrayList<>();
        list.add(new UploadBean(upFile));
        mUploadStrategy.upload(list, true, new UploadCallback() {
            @Override
            public void onFinish(List<UploadBean> list, boolean success) {
                mLoading.dismiss();
                if (success) {
                    if (list != null && list.size() > 0) {
                        mAvatarRemoteFileName = list.get(0).getRemoteFileName();
                        if(commonCallback!=null)
                        commonCallback.callback(mAvatarRemoteFileName);

                    }
                }
            }
        });
    }



}
